<?php

namespace App\Models;

class Projects extends Base
{

    protected $table = 'projects';
    const STATUS_1 = 1;
    const STATUS_2 = 2;
    const STATUS_TEXT = [
        self::STATUS_1 => '<span class="el-tag">正常</span>',
        self::STATUS_2 => '<span class="el-tag el-tag--warning">锁定</span>',
    ];

    const POSITION_1 = 1;
    const POSITION_2 = 2;
    const POSITION_TEXT = [
        self::POSITION_1 => '<span class="el-tag">正常</span>',
        self::POSITION_2 => '<span class="el-tag el-tag--danger">推荐</span>',
    ];

    protected $fillable = [
        'id','thumb','title','catepid','cateid','status','region','explain','online','summary',
        'home_link','white_link','github_link','chrome_link','introduce','member','merit','scene','partner',
        'platform','about_project','about_new','position','view','created_at','updated_at'
    ];
    protected $casts = ['about_project'=>'array','about_new'=>'array'];


    public function cate(){
        return $this->belongsTo('App\Models\Cates', 'cateid', 'id');
    }


    /**
     * 列表
     * @param array $fields
     * @param array $params
     * @param int $limit
     * @return mixed
     */
    protected function list($fields = ['*'], $params = [], $limit = 10){
        return $this->select($fields)
            ->where(function ($query) use ($params) {
                $query ->where('status',Projects::STATUS_1);
                if($params['title']) $query->where('title','like','%'.$params['title'].'%');
            })->with(['cate' => function ($query) {
                $query->select(['id','name'])->get();
            }])->orderBy('created_at','desc')
            ->paginate($limit);
    }


}
